<?php

require_once '../model/Categorie.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class CategorieManager {

    private $bdd; // Instance de PDO

    public function __construct($db) {
        $this->setDb($db);
    }

    public function add(Categorie $categorie) {

        $q = $this->bdd->prepare('INSERT INTO CATEGORIES SET idCat = :idCat, libelle = :libelle');
        $q->bindValue(':idCat', $categorie->getIdCategorie());
        $q->bindValue(':libelle', $categorie->getlibelleCategorie());
        $q->execute();
    }

    public function delete(Categorie $categorie) {
        $id = $categorie->getIdCategorie();
        $this->connexion();
        $bdd->exec('DELETE FROM CATEGORIES WHERE idCat = ' . $id);
    }

    public function update(Categorie $categorie) {
        
    }

    public function getListCategorie() {
        $listCategorie = array();
        $req = $this->bdd->prepare('SELECT * FROM CATEGORIES ');
        $req->execute();
        while ($donnees = $req->fetch()) {
            $categorie = new Categorie($donnees["idCat"], $donnees["libelle"]);
            array_push($listCategorie, $categorie);
        }
        return $listCategorie;
    }

    public function setDb($db) {
        $this->bdd = $db;
    }

    public function getDb() {
        return $this->bdd;
    }
    
    public function getCount ($table, $column) {
        $q = $this->bdd->prepare('SELECT COUNT ('.$column.') FROM '.$table);
        $q->execute();
        $donnees = $q->fetchAll();
        var_dump($donnees);
        return $donnees[0];
        
    }

}
